package com.foreknow.hospital.dao.impl;

import com.foreknow.hospital.dao.HospitalDao;
import com.foreknow.hospital.po.Hospital;
import com.foreknow.hospital.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HospitalDaoImpl implements HospitalDao {
    private Connection con;
    private PreparedStatement pre;

    @Override
    public List<Hospital> listHospitalByOrderTypeId(int orderTypeId) throws SQLException {
    List<Hospital> listBusiness=new ArrayList<Hospital>();
    String sql="select * from hospital where ordertypeId=?"+orderTypeId;
    try{
     con = DBUtil.getConnection();
    //2.获取到预处理对象
    pre = con.prepareStatement(sql);
    pre.setInt(1, orderTypeId);
    ResultSet rs = pre.executeQuery(); //执行查询
    while (rs.next()) {
        Hospital hospital=new Hospital();
        hospital.setHpId(rs.getInt("hpId"));
        hospital.setName(rs.getString("name"));
        hospital.setPicture(rs.getString("picture"));
        hospital.setTelephone(rs.getString("telephone"));
        hospital.setAddress(rs.getString("address"));
        hospital.setBusinessHours(rs.getString("business_hours"));
        hospital.setDeadline(rs.getString("deadline"));
        hospital.setRule(rs.getString("rule"));
        hospital.setState(rs.getInt("state"));
        listBusiness.add(hospital);
    }
} finally {
    DBUtil.close(pre);
}
return listBusiness;

    }

    @Override
    public Hospital getHospitalById(int hpId) throws SQLException {
        Hospital hospital=null;
        String sql="select * from hospital where hpId=?"+hpId;
        try {
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            pre.setInt(1, hpId);
            ResultSet rs = pre.executeQuery(); //执行查询
            while (rs.next()) {
                hospital = new Hospital();
                hospital.setHpId(rs.getInt("hpId"));
                hospital.setName(rs.getString("name"));
                hospital.setPicture(rs.getString("picture"));
                hospital.setTelephone(rs.getString("telephone"));
                hospital.setAddress(rs.getString("address"));
                hospital.setBusinessHours(rs.getString("business_hours"));
                hospital.setDeadline(rs.getString("deadline"));
                hospital.setRule(rs.getString("rule"));
                hospital.setState(rs.getInt("state"));
            }
        } finally{
            DBUtil.close(pre);
        }
        return hospital;
    }
}
